<?php

/**
 * This is the model class for table "tbl_employeedocuments".
 *
 * The followings are the available columns in table 'tbl_employeedocuments':
 * @property integer $id
 * @property string $docname
 * @property string $filename
 * @property string $dateadded
 * @property string $addedby
 * @property string $empno
 * @property string $notes
 */
class Employeedocuments extends CActiveRecord
{
    public $file;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Employeedocuments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_employeedocuments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('docname', 'required'),
			array('docname', 'length', 'max'=>150),
                        array('notes','length','max'=>9999999),
			array('filename, addedby', 'length', 'max'=>255),
                        array('file','file','types'=>'gif,jpg,bmp,png, jpeg,doc, docx, xls, txt, xlsx, ppt, ptt, pdf','maxSize'=>524288000,'allowEmpty'=>true),
			array('empno', 'length', 'max'=>4),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, docname, filename, dateadded, addedby, empno, notes', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'employee'=>array(self::BELONGS_TO,'Employees','empno'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'docname' => 'Document name',
			'filename' => 'Filename',
			'dateadded' => 'Date Added',
			'addedby' => 'Added By',
			'empno' => 'Employee number',
			'notes' => 'Notes',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('docname',$this->docname,true);
		$criteria->compare('filename',$this->filename,true);
		$criteria->compare('dateadded',$this->dateadded,true);
		$criteria->compare('addedby',$this->addedby,true);
		$criteria->compare('empno',$this->empno,true);
		$criteria->compare('notes',$this->notes,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave(){
            
            if($this->isNewRecord){
                $this->dateadded=DateUtil::getDefaultCurrentDateTime();
                $this->addedby=Yii::app()->user->name;
            }
            $this->file=CUploadedFile::getInstance($this, 'file');
            if($this->file!=NULL){
                    //made a random name base on timestamp
                $ext=getFileExt($this->file);
                $name=getFileBodyName($this->file);


                $this->filename='emp-'.$this->empno.'-'.$name.'_'.DateUtil::getDefaultCurrentDateTime().$ext;
                getImageBySize($this->filename,150,150);

                $this->file->saveAs('documents/'.$this->filename,true);   
            }
            return parent::beforeSave();
        }
}